<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>class AboutYou::SDK::Client - RDoc Documentation</title>

<link href="../../fonts.css" rel="stylesheet">
<link href="../../rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../../";
</script>

<script src="../../js/jquery.js"></script>
<script src="../../js/navigation.js"></script>
<script src="../../js/search_index.js"></script>
<script src="../../js/search.js"></script>
<script src="../../js/searcher.js"></script>
<script src="../../js/darkfish.js"></script>


<body id="top" role="document" class="class">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="../../index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="../../table_of_contents.html#pages">Pages</a>
    <a href="../../table_of_contents.html#classes">Classes</a>
    <a href="../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  

  <div id="class-metadata">
    
    <div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  
  <p class="link">Object
  
</div>

    
    
    <!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    
    <li ><a href="#method-c-new">::new</a>
    
    <li ><a href="#method-i-api_endpoint-3D">#api_endpoint=</a>
    
    <li ><a href="#method-i-app_credentials-3D">#app_credentials=</a>
    
    <li ><a href="#method-i-request">#request</a>
    
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="class-AboutYou::SDK::Client">
  <h1 id="class-AboutYou::SDK::Client" class="class">
    class AboutYou::SDK::Client
  </h1>

  <section class="description">
    
<p>This Class manages the API-Calls</p>
<dl class="rdoc-list note-list"><dt>Author
<dd>
<p>Collins GmbH &amp; Co KG</p>
</dd></dl>

  </section>

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    
    <section class="constants-list">
      <header>
        <h3>Constants</h3>
      </header>
      <dl>
      
        <dt id="API_ENDPOINT_LIVE">API_ENDPOINT_LIVE
        
        <dd><p>url for live-api-requests</p>
        
      
        <dt id="API_ENDPOINT_SANDBOX">API_ENDPOINT_SANDBOX
        
        <dd><p>url for sandbox-api-requests</p>
        
      
        <dt id="API_ENDPOINT_STAGE">API_ENDPOINT_STAGE
        
        <dd><p>url for staging-api-requests</p>
        
      
      </dl>
    </section>
    

    
    <section class="attribute-method-details" class="method-section">
      <header>
        <h3>Attributes</h3>
      </header>

      
      <div id="attribute-i-api_endpoint" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">api_endpoint</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        <p>the url used for api calls</p>
        
        </div>
      </div>
      
      <div id="attribute-i-app_id" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">app_id</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        <p>the app id of the app which should be represented by an instance of <a
href="../../AY.html">AY</a></p>
        
        </div>
      </div>
      
      <div id="attribute-i-app_password" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">app_password</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        <p>the authentifaction token from dev-center for the app id</p>
        
        </div>
      </div>
      
      <div id="attribute-i-page_id" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">page_id</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        <p>the page id of the current page visited by an user</p>
        
        </div>
      </div>
      
    </section>
    

    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Class Methods</h3>
       </header>

    
      <div id="method-c-new" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">new</span><span
            class="method-args">( app_id, app_password, _api_endpoint = 'stage', _logger = nil )</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>the Constructor for the <a href="Client.html">Client</a> class</p>
<ul><li>
<p><strong>Args</strong>    :</p>
<ul><li>
<p><code>app_id</code> -&gt; The App-Id of the App</p>
</li><li>
<p><code>app_password</code> -&gt; The Auth-Token of the App</p>
</li><li>
<p><code>api_endpoint</code> -&gt; Can be either live or staging</p>
</li><li>
<p><code>logger</code> -&gt; Logger-Template</p>
</li></ul>
</li><li>
<p><strong>Returns</strong> :</p>
<ul><li>
<p>Instance of <a href="Client.html">AboutYou::SDK::Client</a></p>
</li></ul>
</li></ul>
          
          

          
          <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File lib/AboutYou/client.rb, line 40</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(
  <span class="ruby-identifier">app_id</span>,
  <span class="ruby-identifier">app_password</span>,
  <span class="ruby-identifier">_api_endpoint</span> = <span class="ruby-string">'stage'</span>,
  <span class="ruby-identifier">_logger</span> = <span class="ruby-keyword">nil</span>
)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">app_id</span> = <span class="ruby-identifier">app_id</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">app_password</span> = <span class="ruby-identifier">app_password</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Instance Methods</h3>
       </header>

    
      <div id="method-i-api_endpoint-3D" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">api_endpoint=</span><span
            class="method-args">(api_endpoint)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Setter for the api endpoint</p>
<ul><li>
<p><strong>Args</strong>    :</p>
<ul><li>
<p><code>api_endpoint</code> -&gt; Can be either live or staging</p>
</li></ul>
</li></ul>
          
          

          
          <div class="method-source-code" id="api_endpoint-3D-source">
            <pre><span class="ruby-comment"># File lib/AboutYou/client.rb, line 68</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">api_endpoint=</span>(<span class="ruby-identifier">api_endpoint</span>)
  <span class="ruby-keyword">case</span>
  <span class="ruby-keyword">when</span> <span class="ruby-identifier">api_endpoint</span> <span class="ruby-operator">==</span> <span class="ruby-string">'stage'</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">api_endpoint</span> = <span class="ruby-constant">API_ENDPOINT_STAGE</span>
  <span class="ruby-keyword">when</span> <span class="ruby-identifier">api_endpoint</span> <span class="ruby-operator">==</span> <span class="ruby-string">'sandbox'</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">api_endpoint</span> = <span class="ruby-constant">API_ENDPOINT_SANDBOX</span>
  <span class="ruby-keyword">when</span> <span class="ruby-identifier">api_endpoint</span> <span class="ruby-operator">==</span> <span class="ruby-string">'live'</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">api_endpoint</span> = <span class="ruby-constant">API_ENDPOINT_LIVE</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">api_endpoint</span> = <span class="ruby-identifier">api_endpoint</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-app_credentials-3D" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">app_credentials=</span><span
            class="method-args">(app_id, app_password)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Setter for app-credentials</p>
<ul><li>
<p><strong>Args</strong>    :</p>
<ul><li>
<p><code>app_id</code> -&gt; The App-Id of the App</p>
</li><li>
<p><code>app_password</code> -&gt; The Auth-Token of the App</p>
</li></ul>
</li></ul>
          
          

          
          <div class="method-source-code" id="app_credentials-3D-source">
            <pre><span class="ruby-comment"># File lib/AboutYou/client.rb, line 57</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">app_credentials=</span>(<span class="ruby-identifier">app_id</span>, <span class="ruby-identifier">app_password</span>)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">app_id</span> = <span class="ruby-identifier">app_id</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">app_password</span> = <span class="ruby-identifier">app_password</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-request" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">request</span><span
            class="method-args">(body)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Builds a JSON string representing the request-data Executes the API request
builds a JSON string representing the response-data</p>
<ul><li>
<p><strong>Args</strong>    :</p>
<ul><li>
<p><code>body</code> -&gt; the body of the api-call, containing all request
data</p>
</li></ul>
</li><li>
<p><strong>Fails</strong> :</p>
<ul><li>
<p>if the http response code is not between 200 and 300 or 304</p>
</li><li>
<p>if the body of the http response is not an array</p>
</li></ul>
</li><li>
<p><strong>Returns</strong> :</p>
<ul><li>
<p>Json-String containing the response-body</p>
</li></ul>
</li></ul>
          
          

          
          <div class="method-source-code" id="request-source">
            <pre><span class="ruby-comment"># File lib/AboutYou/client.rb, line 96</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">request</span>(<span class="ruby-identifier">body</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">page_id</span>
    <span class="ruby-identifier">request</span> = {
      <span class="ruby-identifier">body</span><span class="ruby-operator">:</span> <span class="ruby-identifier">body</span>,
      <span class="ruby-identifier">basic_auth</span><span class="ruby-operator">:</span> {
        <span class="ruby-identifier">username</span><span class="ruby-operator">:</span> <span class="ruby-identifier">app_id</span>,
        <span class="ruby-identifier">password</span><span class="ruby-operator">:</span> <span class="ruby-identifier">app_password</span>
      },
      <span class="ruby-identifier">headers</span><span class="ruby-operator">:</span> {
        <span class="ruby-string">'Content-Type'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'application/json'</span>,
        <span class="ruby-string">'Accept-Encoding'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'gzip,deflate'</span>,
        <span class="ruby-string">'X-Page-ID'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">page_id</span>
      }
    }
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">request</span> = {
      <span class="ruby-identifier">body</span><span class="ruby-operator">:</span> <span class="ruby-identifier">body</span>,
      <span class="ruby-identifier">basic_auth</span><span class="ruby-operator">:</span> {
        <span class="ruby-identifier">username</span><span class="ruby-operator">:</span> <span class="ruby-identifier">app_id</span>,
        <span class="ruby-identifier">password</span><span class="ruby-operator">:</span> <span class="ruby-identifier">app_password</span>
      },
      <span class="ruby-identifier">headers</span><span class="ruby-operator">:</span> {
        <span class="ruby-string">'Content-Type'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'application/json'</span>,
        <span class="ruby-string">'Accept-Encoding'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">'gzip,deflate'</span>
      }
    }
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">response</span> = <span class="ruby-constant">HTTParty</span>.<span class="ruby-identifier">post</span>(<span class="ruby-string">'https://shop-api.aboutyou.de/api'</span>, <span class="ruby-identifier">request</span>)
  <span class="ruby-identifier">code</span> = <span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span>

  <span class="ruby-identifier">fail</span> <span class="ruby-constant">String</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span>) <span class="ruby-keyword">unless</span>
  <span class="ruby-identifier">code</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">200</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">code</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">300</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">code</span> <span class="ruby-operator">==</span> <span class="ruby-value">304</span>
  <span class="ruby-identifier">fail</span> <span class="ruby-string">'result is not array'</span> <span class="ruby-keyword">unless</span> <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>).<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)

  <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>)
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
  </section>
</main>


<footer id="validator-badges" role="contentinfo">
  <p><a href="http://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="http://rdoc.rubyforge.org">RDoc</a> 4.1.2.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>

